package com.sunshine.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import de.robv.android.xposed.XposedHelpers;

/**
 * Description:
 * File: HookUtil.java
 * Create: 2018/11/07 09:29
 *
 * @author Administrator
 */
public class HookUtil {
    private static final String DATE_FORMAT_KEY = "yyyyMMddHHmmss";

    public static String getVerName(Context context) {
        String versionName = null;
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * @param source Object
     * @param key    String
     * @param arg6   Object
     * @return boolean
     */
    public static boolean setFieldValue(Object source, String key, Object arg6) {
        boolean v2 = false;
        if (source != null) {
            Field v1 = XposedHelpers.findField(source.getClass(), key);
            if (v1 == null) {
                return false;
            }

            try {
                v1.set(source, arg6);
                v2 = true;
            } catch (Exception ignored) {
            }
        }
        return v2;
    }

    /**
     * @param source Object
     * @param key    key
     * @return Object
     */
    public static Object getFieldValue(Object source, String key) {
        Object v2 = null;
        if (source != null) {
            Field v1 = XposedHelpers.findField(source.getClass(), key);
            if (v1 == null) {
                return null;
            }
            try {
                v2 = v1.get(source);
            } catch (Exception ignored) {
            }
        }
        return v2;
    }

    /**
     * @param arg2 String
     * @return String
     */
    public static String getCrowdNoFromLink(String arg2) {
        return (String) getQueryParam(arg2).get("crowdNo");
    }

    private static Map getQueryParam(String arg11) {
        HashMap<String, String> v2 = new HashMap<>();
        String v4 = truncateUrlPage(arg11);
        if (v4 != null) {
            String[] v0 = v4.split("[&]");
            int v7 = v0.length;
            int v5;
            for (v5 = 0; v5 < v7; ++v5) {
                String[] v1 = v0[v5].split("[=]");
                if (v1.length > 1) {
                    v2.put(v1[0], v1[1]);
                } else if (!"".equals(v1[0])) {
                    v2.put(v1[0], "");
                }
            }
        }

        return v2;
    }

    private static String truncateUrlPage(String arg4) {
        String v1 = null;
        arg4 = arg4.trim();
        String[] v0 = arg4.split("[?]");
        if (arg4.length() > 1 && v0.length > 1 && v0[1] != null) {
            v1 = v0[1];
        }
        return v1;
    }

    static void startReceiveRedPacket(String arg2, String arg3, Context arg4) {
        startApp("88886666", linkToBundle(arg2, arg3), arg4);
    }

    private static void startApp(String arg10, Bundle arg11, Context arg12) {
        try {
            XposedHelpers.callMethod(XposedHelpers.callMethod(XposedHelpers.callStaticMethod(XposedHelpers.findClass("com.alipay.mobile.framework.AlipayApplication", arg12.getClassLoader()), "getInstance"), "getMicroApplicationContext"), "startApp", new Class[]{String.class, String.class, Bundle.class}, null, arg10, arg11);
        } catch (Exception v2) {
            LogUtil.log("StartApp error : " + v2.toString());
        }
    }

    private static Bundle linkToBundle(String arg5, String arg6) {
        Bundle v2 = new Bundle();
        Map v1 = getQueryParam(arg5);
        for (Object v0 : v1.keySet()) {
            v2.putString(((String) v0), (String) v1.get(v0));
        }
        v2.putString("socialCardCMsgId", arg6);
        v2.putBoolean("startFromExternal", true);
        return v2;
    }

    public static String getFormat(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_KEY, Locale.getDefault());
        return dateFormat.format(date);
    }
}
